<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Boost搜索引擎</title>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <style>
        /* 去掉网页的默认边距 */
        * {
            /* 外边距 */
            margin: 0;
             /*内边距  */
            padding: 0;
        }
        /* 设置body内容与html相同 */
        html,
        body{
            height: 100%;
        }
        .container{
            width: 800px;
            /* 通过设置外边距达到居中对齐的目的 */
            margin: 0px auto;
            margin-top: 20px;
        }

        .container .search{
            width: 100%; /* 宽度与父标签一致*/
            height: 52px;
        }

        .container .search input{
            float: left;
            width: 600px;
            height: 50px;
            border: 2px solid #4e6ef2;
            border-right: none;
            padding-left: 10px;
            color: rgb(0, 0, 0);
            font-size: 18px;
            outline: none;
        }

        .container .search button:active{
            transform: scale(0.98);
            box-shadow: 3px 2px 22px 1px rgba(0, 0, 0, 0.24);
        }

        .container .search button:hover{
            background: RGB(70,98,217);
            color: #FFF;
            border: none;
        }

        .container .search button{
            width: 150px;
            float: left;
            height: 54px;
            background-color: #4e6ef2;
            color: #FFF;
            font-size: 19px;
            border: 2px #4e6ef2;
        }

        .container .result{
            width: 100%;
        }

        .container .result .item{
            margin-top: 15px;
        }
        .container .result .item a{
            /* 块级元素，一次占一行 */
            display: block;
            /* 去标签 */
            text-decoration: none;
            font-size: 20px;
            color: #001ba0;
        }
        /* 鼠标放上去有下划线 */
        .container .result .item a:hover {
            text-decoration: underline;
        }

        .container .result .item p{
            margin-top: 5px;
            font-size: 15px;
        }

        .container .result .item i{
            display: block;
            /* 取消斜体 */
            font-style: normal;
            color: green;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="search">
            <input type="text" value="请输入搜索关键字...">
            <button onclick="Search()">搜索一下</button>
        </div>
        <div class="result">
            <!-- <div class="item">
                <a href="#">测试专用</a>
                <p>测试专用：这是摘要</p>
                <i>https://www.jq22.com/cdn/#a2</i>
            </div> -->
        </div>
    </div>

    
    <script>
        function Search(){
            // 是浏览器的一个弹出框
            // alert("hello js!");
            // 1. 提取数据, $可以理解成就是JQuery的别称
            let query = $(".container .search input").val();
            console.log("query = " + query); //console是浏览器的对话框，可以用来进行查看js数据

            //2. 发起http请求,ajax: 属于一个和后端进行数据交互的函数，JQuery中的
            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function(data){
                    console.log(data);
                    BuildHtml(data);
                }
            });
        }

        function BuildHtml(data){
            // 获取html中的result标签
            let result_lable = $(".container .result");
            // 清空历史搜索结果
            result_lable.empty();

            for( let elem of data){
                // console.log(elem.title);
                // console.log(elem.url);
                let a_lable = $("<a>", {
                    text: elem.title,
                    href: elem.url,
                    // 跳转到新的页面
                    target: "_blank"
                });
                let p_lable = $("<p>", {
                    text: elem.desc
                });
                let i_lable = $("<i>", {
                    text: elem.url
                });
                let div_lable = $("<div>", {
                    class: "item"
                });
                a_lable.appendTo(div_lable);
                p_lable.appendTo(div_lable);
                i_lable.appendTo(div_lable);
                div_lable.appendTo(result_lable);
            }
        }
    </script>
</body>
</html>